﻿/*************************************************************************************
 * 文 件 名:   HK_EnumRestrictAttribute
 * 
 * 描    述:   Enum枚举类型显示时，进行裁剪，填入初始索引和最终索引
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2023/8/17
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Linq;
using UnityEditor;
using UnityEngine;

namespace HKTools
{
    public class HK_EnumRestrictAttribute : PropertyAttribute
    {
        public int startIndex, endIndex;

        public HK_EnumRestrictAttribute(int startIndex, int endIndex)
        {
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }
    }

#if UNITY_EDITOR
    [CustomPropertyDrawer(typeof(HK_EnumRestrictAttribute))]
    public class HK_EnumRestrictDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            HK_EnumRestrictAttribute enumRestriction = attribute as HK_EnumRestrictAttribute;

            string[] enumNames = property.enumNames.Skip(enumRestriction.startIndex).Take(enumRestriction.endIndex - enumRestriction.startIndex).ToArray();

            int[] allowedIndices = new int[enumRestriction.endIndex - enumRestriction.startIndex];

            for (int i = 0; i < enumRestriction.endIndex - enumRestriction.startIndex; i++)
            {
                allowedIndices[i] = enumRestriction.startIndex + i;
            }

            EditorGUI.BeginProperty(position, label, property);
            property.enumValueIndex = EditorGUI.IntPopup(position, label.text, property.enumValueIndex, enumNames, allowedIndices);
            EditorGUI.EndProperty();
        }
    }
#endif
}